<template>
	<view class="template">
		<create-poster ref="createPoster" :goodsSpu="goodsSpu" v-if="showPoster" @closePoster="closePoster"
			:gType="gType" :activityId="activityId"></create-poster>
		<create-urlScheme ref="createUrlScheme" :goodsSpu="goodsSpu" :gType="gType"
			:activityId="activityId"></create-urlScheme>
		<create-share-video ref="createUrlVideo" :goodsSpu="goodsSpu" :gType="gType"
			:activityId="activityId"></create-share-video>
		<view :class="'cu-modal bottom-modal ' + shareShowPop" @tap="shareHide" v-if="shareShowPop">
			<view class="cu-dialog" @tap.stop>
				<view class="cu-bar bg-white">
					<view class="action text-green"></view>
					<view class="action text-red" @tap="shareHide">取消</view>
				</view>
				<view class="padding flex flex-direction">
					<button class="cu-btn margin-tb-sm lg round shadow-blur text-white"
						v-if="goodsSpu.spuShare&&goodsSpu.spuShare.videoUrl&&goodsSpu.spuShare.status=='1'"
						style="background-color:#4F91F5;" @tap="onCreatePoster(3)">分享商品视频介绍</button>
					<button class="cu-btn margin-tb-sm lg round shadow-blur text-white"
						style="background-color:#39B54A;" @tap="onCreatePoster(1)">生成短链接</button>
					<button class="cu-btn margin-tb-sm lg round shadow-blur text-white"
						style="background-color:#E53E3D;" @tap="onCreatePoster(2)">
						生成海报
					</button>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import createPoster from '../create-poster/create-poster.vue'
	import createUrlScheme from '../create-poster/create-urlScheme.vue'
	import createShareVideo from '../create-poster/create-share-video.vue'
	import api from 'utils/api'

	export default {
		name: 'create-share',
		data() {
			return {
				shareShowPop: '',
				showPoster: false
			}
		},
		props: {
			goodsSpu: {
				type: Object,
				default: () => {}
			},
			gType: {
				type: [String || null],
				default: ''
			},
			activityId: {
				type: [String || null],
				default: ''
			}
		},
		components: {
			createPoster,
			createUrlScheme,
			createShareVideo
		},
		methods: {
			openSharePop() {
				if (!this.goodsSpu || !this.goodsSpu.skus || !this.goodsSpu.skus.length) {
					uni.showModal({
						title: '提示',
						content: '抱歉，该商品已下架',
						showCancel: false
					})
				} else if (this.activityId) {
					api.checkActivity({
						activityId: this.activityId,
						skuId: this.goodsSpu.skus[0].id
					}).then(res => {
						const obj = res && res.data ? res.data : {};
						if (obj.activity == 1 && obj.goods == 1) {
							this.shareShowPop = 'show'
						} else {
							uni.showModal({
								title: '提示',
								content: '抱歉，该商品已下架',
								showCancel: false
							})
						}
					})
				} else {
					this.shareShowPop = 'show'
				}
			},
			shareHide() {
				this.shareShowPop = '';
			},
			onCreatePoster(flag) {
				const userInfo = uni.getStorageSync('user_info');
				if (userInfo && userInfo.id) {
					this.beginShare(flag)
				} else {
					uni.login({
						success: (res) => {
							if (res.code) {
								api.loginWxMa2({
									jsCode: res.code
								}).then(res => {
									uni.setStorageSync('third_session', res.data.thirdSession);
									uni.setStorageSync('user_info', res.data);
									this.beginShare(flag)
								});
							}
						}
					});
				}
			},
			beginShare(flag) {
				this.shareShowPop = ''
				if (flag == 1) {
					this.$refs.createUrlScheme.shareUrlScheme()
				} else if (flag == 3) {
					this.$refs.createUrlVideo.shareUrlVideo()
				} else {
					uni.getSetting({
						success: (res) => {
							if (res.authSetting['scope.writePhotosAlbum']) {
								this.showPoster = true
								this.$nextTick(() => {
									this.$refs.createPoster.sharePost()
								})
							} else if (!res.authSetting.hasOwnProperty("scope.writePhotosAlbum")) {
								uni.authorize({
									scope: 'scope.writePhotosAlbum',
									success: () => {
										this.showPoster = true
										this.$nextTick(() => {
											this.$refs.createPoster.sharePost()
										})
									}
								})
							} else {
								uni.showModal({
									title: "小程序未授权使用相册",
									content: "请先在设置中打开“添加到相册”开关，然后再点击'分享'按钮",
									confirmColor: "#e5432e",
									confirmText: "去设置",
									success(res) {
										if (res.confirm) {
											uni.openSetting({})
										}
									}
								})
							}
						},
						fail(res) {
							console.log(res, 'uni.getSetting');
						}
					})
				}
			},
			// 分享组件销毁			
			closePoster() {
				this.showPoster = false
			}
		}
	}
</script>

<style>
</style>